package com.thomas.framework.controller.admin;

import com.thomas.framework.core.annotation.Function;
import com.thomas.framework.core.annotation.FunctionMenu;
import com.thomas.framework.entity.SysRole;
import com.thomas.framework.entity.SysUser;
import com.thomas.framework.service.ISysRoleService;
import com.thomas.framework.service.ISysUserService;
import com.thomas.framework.service.impl.SysRoleServiceImpl;
import com.thomas.framework.utils.CommonUtil;
import com.thomas.framework.utils.JsonUtil;
import com.thomas.framework.utils.PageResult;
import com.thomas.framework.utils.mybatis.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.HashSet;

/**
 * SysUser Controller
 * Created by taohj on 2014/4/20.
 */

@Controller
@RequestMapping("admin/sys/user/")
@FunctionMenu("SysUserManagement")
public class SysUserController {

    private ISysUserService service;

    private ISysRoleService roleService;


    @Autowired
    public void setRoleService(ISysRoleService roleService) {
        this.roleService = roleService;
    }

    /**
     * 用户服务层对象
     */
    @Autowired
    public void setService(ISysUserService service) {
        this.service = service;
    }


    /**
     * 用户新增页面
     */

    @Function("SysUserCreateView")
    @RequestMapping("createView")
    public String createView() {
        return "admin/sys/user/create";
    }


    /**
     * 用户新增
     */
    @Function("SysUserCreate")
    @ResponseBody
    @RequestMapping("create")
    public String create(
          HttpServletRequest request
    ) throws ParseException {
        PageResult pageResult = new PageResult();
        SysUser user =this.initSysUser(request);
        pageResult.setSuccess(service.create(user));
        return pageResult.toString();
    }


    /**
     * 用户删除操作
     */
    @Function("SysUserRemove")
    @ResponseBody
    @RequestMapping("remove")
    public String remove(@RequestParam("id") String id) {
        PageResult pageResult = new PageResult();
        SysUser user = new SysUser();
        user.setId(id);
        pageResult.setSuccess(service.remove(user));
        return pageResult.toString();
    }


    /**
     * 用户修改页面
     */


    @Function("SysUserModifyView")
    @RequestMapping("modifyView")
    public String modifyView(
            HttpServletRequest request,
            @RequestParam("id") String id
    ) {
        SysUser user =service.getById(new SysUser(id));
        request.setAttribute("user", user);

        return "admin/sys/user/modify";
    }


    /**
     * 用户修改
     */
    @Function("SysUserModify")
    @ResponseBody
    @RequestMapping("modify")
    public String modify(
           HttpServletRequest request
    ) throws ParseException {
        PageResult pageResult = new PageResult();

        SysUser user = this.initSysUser(request);
        pageResult.setSuccess(service.modify(user));
        return pageResult.toString();

    }


    /**
     * 用户列表页面
     */
    @Function("SysUserManagement")
    @RequestMapping("management")
    public String listView() {
        return "admin/sys/user/management";
    }



    @Function("SysUserManagement")
    @ResponseBody
    @RequestMapping("list")
    public String list(
         HttpServletRequest request


    ) {
        PageResult<SysUser> pageResult = new PageResult<SysUser>();
        CommonUtil.initPageResult(pageResult,request);
        pageResult.setT(this.initSysUser(request));
        pageResult = service.getByPage(pageResult);
        return JsonUtil.toString(pageResult);
    }



    @Function("SysUserAssignedRolesView")
    @RequestMapping("assignedRolesView")
    public String assignedRolesView(
            HttpServletRequest request,
            @RequestParam("id") String id
    ){
        request.setAttribute("id",id);
        return  "admin/sys/user/assignedRoles";
    }


    @Function("SysUserAssignedRolesList")
    @ResponseBody
    @RequestMapping("assignedRolesList")
    public String assignedRolesList(
            HttpServletRequest request,
            @RequestParam("id") String id,
            @RequestParam(value = "roleid",required = false) String roleid,
            @RequestParam(value = "roleName",required = false) String roleName

    ){
        PageResult<SysRole> pageResult=new PageResult<SysRole>();
        CommonUtil.initPageResult(pageResult,request);
        pageResult.setT(new SysRole(roleid,roleName,null));
        pageResult.setRows(roleService.getRolesByUser(new SysUser(id)));
        return  pageResult.toString();
    }



    /**
     * 赋予用户角色
     */
    @Function("SysUserAssignedRoles")
    @ResponseBody
    @RequestMapping("assignedRoles")
    public String assignedRoles(
            HttpSession session,
            @RequestParam("id") String id,
            @RequestParam(value = "roleid",required = false) String [] roleids
    ) {

        SysUser currentUser= (SysUser) session.getAttribute("user");

        SysUser user=new SysUser(id);
        if (roleids!=null && roleids.length>0){
            user.setRoles(new HashSet<SysRole>());
            for(String roleid : roleids){
                user.getRoles().add(new SysRole(roleid));
            }
        }
        JsonResult result=new JsonResult();
        result.setSuccess(service.assignedRoles(user,currentUser));
        return result.toString();

    }



    private  SysUser initSysUser(HttpServletRequest request){
       SysUser user=new SysUser();
       user.setId(request.getParameter("id"));
       user.setRealName(request.getParameter("realName"));
       user.setNickName(request.getParameter("nickName"));
       user.setUserName(request.getParameter("userName"));
       user.setPassword(request.getParameter("password"));
       user.setSex(request.getParameter("sex"));
       user.setBirthday(CommonUtil.dateParse(request.getParameter("birthday")));
       user.setAddress(request.getParameter("address"));
       user.setPhone(request.getParameter("phone"));
       user.setEmail(request.getParameter("email"));
       user.setQQ(request.getParameter("QQ"));
       user.setHeadImage(request.getParameter("headImage"));
        user.setStatus(request.getParameter("status"));



       user.setId(StringUtils.hasLength(user.getId())?user.getId():null);
       user.setRealName(StringUtils.hasLength(user.getRealName())?user.getRealName():null);
       user.setNickName(StringUtils.hasLength(user.getNickName())?user.getNickName():null);
       user.setUserName(StringUtils.hasLength(user.getUserName())?user.getUserName():null);
       user.setPassword(StringUtils.hasLength(user.getPassword())?user.getPassword():null);
       user.setSex(StringUtils.hasLength(user.getSex())?user.getSex():null);
       /*user.setBirthday(StringUtils.hasLength(user.getBirthday())?user.getBirthday():null);*/
       user.setAddress(StringUtils.hasLength(user.getAddress())?user.getAddress():null);
       user.setPhone(StringUtils.hasLength(user.getPhone())?user.getPhone():null);
       user.setEmail(StringUtils.hasLength(user.getEmail())?user.getEmail():null);
       user.setQQ(StringUtils.hasLength(user.getQQ())?user.getQQ():null);
       user.setHeadImage(StringUtils.hasLength(user.getHeadImage())?user.getHeadImage():null);
       user.setStatus(StringUtils.hasLength(user.getStatus())?user.getStatus():null);
        return user;
    }






}
